﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UnitVariables;

using System.Globalization;

namespace UnitVariables
{
    /// <summary>
    /// Interaction logic for UVC.xaml
    /// </summary>
    public partial class UVC : UserControl
    {
        //UnitVarib localHold = new UnitVarib(12, "m");
        public UVC()
        {
            InitializeComponent();        
        }

        public static readonly DependencyProperty VarNameProperty = DependencyProperty.Register("VarName", typeof(string), typeof(UVC));
        public string VarName 
        {
            get { return (string)GetValue(VarNameProperty); }
            set { SetValue(VarNameProperty, value); }
        }

        public static readonly DependencyProperty VarProperty = DependencyProperty.Register("Var", typeof(UnitVarib), typeof(UVC));
        public UnitVarib Var
        {
            get { return (UnitVarib)GetValue(VarProperty); }
            set { SetValue(VarProperty, value); } 
        }

        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UVC));
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }       

        public static readonly DependencyProperty UnitsWidthProperty =
                    DependencyProperty.Register("UnitsWidth", typeof(int), typeof(UVC), new PropertyMetadata(50));
        public int UnitsWidth
        {
            get { return (int)GetValue(UnitsWidthProperty); }
            set { SetValue(UnitsWidthProperty, value); }
        }

        public static readonly DependencyProperty ValueWidthProperty =
            DependencyProperty.Register("ValueWidth", typeof(int), typeof(UVC), new PropertyMetadata(50));
        public int ValueWidth
        {
            get { return (int)GetValue(ValueWidthProperty); }
            set { SetValue(ValueWidthProperty, value); }
        }

        public static readonly DependencyProperty VarWidthProperty =
            DependencyProperty.Register("VarWidth", typeof(int), typeof(UVC), new PropertyMetadata(100));
        public int VarWidth
        {
            get { return (int)GetValue(VarWidthProperty); }
            set { SetValue(VarWidthProperty, value); }
        }

        public static readonly RoutedEvent TextChangedEvent = EventManager.RegisterRoutedEvent(
            "TextChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UVC));

        public event RoutedEventHandler TextChanged
        {
            add { AddHandler(TextChangedEvent, value); }
            remove { RemoveHandler(TextChangedEvent, value); } 
        }

        void RaiseTextChangedEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(UVC.TextChangedEvent);
            RaiseEvent(newEventArgs);
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            RaiseTextChangedEvent();
        }

    }

    [ValueConversion(typeof(string), typeof(String))]
    public class ColonAdder: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (string)value + ':';
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //throw new NotImplementedException();
            string str = (string)value;

            if (str[str.Length - 1] == ':')
                return str.Substring(0, str.Length - 2);
            else
                return str;
        }
    }

}
